<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\SystemAdmin;
use app\common\model\TopicLog;
use app\common\model\User as ModelUser;
use think\facade\View;
use think\facade\Config;
use think\facade\Cache;


class User extends Base
{
    public function initialize()
    {
        // parent::initialize();

        $this->user = new ModelUser;
        $this->topiclog = new TopicLog;
        $this->systemadmin = new SystemAdmin;
    }
    public function userlist()
    {
        if (request()->IsAjax()) {
            $get = $this->request->get();
            $field = '';
            $sort = 'add_time desc';
            $where = [];
            if (isset($get['searchParams'])) {
                $search = json_decode($get['searchParams'], true);
                if (isset($search['nickname']) && $search['nickname']) {
                    $where[] = ['nickname', 'like', '%' . $search['nickname'] . '%'];
                }
                
            }
            $list = $this->user->getPagelist($where, $field, $sort);
            foreach($list as &$v){
                $v['headimg'] = $v['headimg']?:"/system/default.png";
                $v['nums'] = $this->topiclog->countRows(['user_id' => $v['id']]);
                $v['identity'] = $this->systemadmin->countRows(['user_id' => $v['id'],'status'=>1])>0?'管理员':'普通用户';
            }
            $count = $this->user->countRows($where);
            return json(['code' => 0, 'data' => $list, 'count' => $count]);
        }
        return View::fetch();
    }
    public function userone(){
        $get = $this->request->get();
        // $id = $get['id'];
        $one = $this->user->getOne($get);
        $one['headimg'] = $one['headimg']?:"/system/default.png";
        $one['identity'] = $this->systemadmin->countRows(['user_id' => $one['id'],'status'=>1])>0?'管理员':'普通用户';
        View::assign('one', $one);
        return View::fetch();
    }

    public function topiclog()
    {
        if (request()->IsAjax()) {
            $get = $this->request->get();
            $where = [];
            $where[] = ['user_id', '=', $get['user_id']];
            $field = 'a.id,a.inTime,a.naire_title,b.nickname,b.headimg';
            $order = 'id desc';

            $list = $this->topiclog->joinUser($where, $field, $order);
            $count = $this->topiclog->countRows($where);
            foreach ($list as &$v) {
                $v['inTime'] = date('Y-m-d H:i:s', $v['inTime']);
                $v['headimg'] = $v['headimg'] ?: "/system/default.png";
            }
            return json(['code' => 0, 'data' => $list, 'count' => $count]);
        }
        // $naire = $this->naire->getAll([]);
        // $this->assign('naire', $naire);
        return  View::fetch();
    }


    public function welcome()
    {
        // return View::fetch();
    }
}
